package MapReduce.Demo3_InputFormat;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import utils.JobSubmit;

import java.io.IOException;

/**
 * @Author lixin
 * @Date 2023/3/15 10:56
 */
public class CombineFileInputFormatApp {

    public static class CFIFMapper extends Mapper<LongWritable, Text,Text, IntWritable>{

        Text outKey = new Text();
        IntWritable outValue = new IntWritable(1);

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            String line = value.toString();
            String[] words = line.split(",");

            outKey.set(words[0]);
            outValue.set(Integer.parseInt(words[1]));

            context.write(outKey,outValue);

        }
    }

    public static class CFIFReducer extends Reducer<Text,IntWritable,Text,IntWritable> {


        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            for (IntWritable value : values) {
                context.write(key,value);
            }


        }
    }


    public static void main(String[] args) throws Exception {

//        Configuration conf = new Configuration();
//
//        Job job = Job.getInstance(conf);
//
//        job.setMapOutputKeyClass(Text.class);
//        job.setMapOutputValueClass(IntWritable.class);
//        job.setOutputKeyClass(Text.class);
//        job.setOutputValueClass(IntWritable.class);
//
//        job.setJarByClass(TextInputFormatApp.class);
//        job.setMapperClass(TTFMapper.class);
//        job.setReducerClass(TTFReducer.class);
//
//
//        Path outputPath = new Path(args[1]);
//
//        FileSystem fs = FileSystem.get(conf);
//
//        if (fs.exists(outputPath)) {
//            fs.delete(outputPath,true);
//        }
//
//        CombineFileInputFormat.setMaxInputSplitSize(job,);
//        //设置输入格式化的类型，如果不设置，模式任用TextInputFormat
//        job.setInputFormatClass(CombineTextInputFormat.class);
//
//        FileInputFormat.setInputPaths(job,new Path(args[0]));
//        FileOutputFormat.setOutputPath(job,outputPath);
//
//        job.waitForCompletion(true);


        JobSubmit.setMaxSplitSize(90);
        JobSubmit.submitBaseJob(CombineFileInputFormatApp.class,args, CombineTextInputFormat.class);

    }

}
